package cisdi.mes.sm.rm.daoImpl;

import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cisdi.mes.sm.rm.dao.StorageContentDao;
import cisdi.mes.sm.rm.domain.SmRmMeasureDoc;
import cisdi.mes.sm.rm.domain.SmRmStorageContent;
import cisdi.mes.sm.util.base.Arith;

@Repository
public class StorageContentDaoImpl extends HibernateDaoSupport implements
		StorageContentDao {

	@Autowired
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	@Override
	public void store(SmRmMeasureDoc md, String storageId, Double wgt) {
		SmRmStorageContent storageContent = new SmRmStorageContent();
		storageContent.setMeasureId(md.getMeasureId());
		storageContent.setMaterialId(md.getMaterialId());
		storageContent.setMaterialDesc(md.getMaterialDesc());
		storageContent.setQualityBatch(md.getQualityBatch());
		storageContent.setStorageId(storageId);
		storageContent.setWeight(wgt);
		storageContent.setTimeStamp(new Date());
		this.getHibernateTemplate().save(storageContent);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Double getStorageWgt(String storageId) {
		String queryString = "select sum(t.weight)";
		queryString += " from SmRmStorageContent t";
		queryString += " where t.storageId = '" + storageId + "'";
		List list = this.getHibernateTemplate().find(queryString);
		return Double.valueOf(list.get(0).toString());
	}
	
	@Override
	public SmRmStorageContent getById(long contentId) {		
		return this.getHibernateTemplate().get(SmRmStorageContent.class, contentId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SmRmStorageContent> getContents(String storageId) {
		String queryString = "from SmRmStorageContent t";
		queryString += " where t.storageId = '" + storageId + "'";
		queryString += " order by t.timeStamp asc";
		return this.getHibernateTemplate().find(queryString);
	}

	@Override
	public void deleteContent(SmRmStorageContent content) {
		this.getHibernateTemplate().delete(content);
	}

	@Override
	public void decreaseWgt(SmRmStorageContent content, Double wgt) {
		Double currWgt = content.getWeight();
		Double newWgt = Arith.sub(currWgt, wgt);
		content.setWeight(newWgt);
		this.getHibernateTemplate().update(content);
	}

	@Override
	public void addContent(String to, SmRmStorageContent from) {
		SmRmStorageContent content = new SmRmStorageContent();
		content.setStorageId(to);
		content.setMeasureId(from.getMeasureId());
		content.setMaterialId(from.getMaterialId());
		content.setMaterialDesc(from.getMaterialDesc());
		content.setBatchId(from.getBatchId());
		content.setQualityBatch(from.getQualityBatch());
		content.setWeight(from.getWeight());
		content.setTimeStamp(new Date());
		// 保存源料仓库存基本数据，用于回退操作
		content.setFromContentId(from.getContentId());
		content.setFromStorageId(from.getStorageId());
		content.setFromTimeStamp(from.getTimeStamp());
		this.getHibernateTemplate().save(content);
	}

	@Override
	public void addContent(String to, SmRmStorageContent from, Double wgt) {
		SmRmStorageContent content = new SmRmStorageContent();
		content.setStorageId(to);
		content.setMeasureId(from.getMeasureId());
		content.setMaterialId(from.getMaterialId());
		content.setMaterialDesc(from.getMaterialDesc());
		content.setBatchId(from.getBatchId());
		content.setQualityBatch(from.getQualityBatch());
		content.setWeight(wgt);
		content.setTimeStamp(new Date());
		// 保存源料仓库存基本数据，用于回退操作
		content.setFromContentId(from.getContentId());
		content.setFromStorageId(from.getStorageId());
		content.setFromTimeStamp(from.getTimeStamp());
		this.getHibernateTemplate().save(content);
	}
	
	@Override
	public void backContent(SmRmStorageContent content, Double wgt) {
		Double weight1 = content.getWeight();
		Double weight2 = Arith.add(weight1, wgt);
		content.setWeight(weight2);		
		this.getHibernateTemplate().update(content);
	}
	
	@Override
	public void backContent(SmRmStorageContent content) {
		SmRmStorageContent dwContent = new SmRmStorageContent();
		dwContent.setContentId(content.getFromContentId());
		dwContent.setStorageId(content.getFromStorageId());
		dwContent.setMaterialId(content.getMaterialId());
		dwContent.setMaterialDesc(content.getMaterialDesc());
		dwContent.setMeasureId(content.getMeasureId());
		dwContent.setWeight(content.getWeight());
		dwContent.setTimeStamp(content.getFromTimeStamp());
		this.getHibernateTemplate().save(dwContent);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SmRmStorageContent> findByStorageIdAndMaterialId(
			String storageId, String materialId) {
		String queryString = "from SmRmStorageContent t";
		queryString += " where t.storageId=:storageId and t.materialId=:materialId";
		queryString += " order by t.timeStamp asc";
		String[] paramNames = { "storageId", "materialId" };
		Object[] values = { storageId, materialId };
		return this.getHibernateTemplate().findByNamedParam(queryString,
				paramNames, values);
	}
}
